package cn.hs.controller;

import cn.hs.entity.User;
import cn.hs.service.UserService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author mujiale
 * @Date 2024年10月24日 10:08
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    //查询所有并分页与模糊查询
    public Result select(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            User user
    ){
        PageInfo<User> userPageInfo = userService.selectAll(pageNum, pageSize, user);
        return Result.success(userPageInfo);
    }
    @PostMapping
    //增加
    public Result insert(@RequestBody User user){
        int num = userService.insert(user);
        if (num != 0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
    @PutMapping
    //修改
    public Result update(@RequestBody User user){
        int num = userService.update(user);
        if (num != 0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    //删除
    public Result delete(@PathVariable("id") Integer id){
        int num = userService.delete(id);
        if (num != 0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/findAll")
    //光查询所有
    public Result selectAll(User user){
        List<User> select = userService.select(user);
        return Result.success(select);
    }
    @PostMapping("/findNameAndPwd")
    public Result selectNameAndPwd(User user){
        User user1 = userService.selectNameAndPwd(user.getUsername(), user.getPassword());
        if (user1 != null){
            return Result.success(user1);
        }else {
            return Result.error("账号或密码错误");
        }
    }
}
